package com.note.chh.notebook.UI.enjoy;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.note.chh.notebook.Adapter.VideoAdapter;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Uitls.ItemDivider;
import com.note.chh.notebook.Uitls.localFile.FileManager;
import com.note.chh.notebook.Uitls.localFile.bean.VideoItem;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.util.List;

import butterknife.BindView;

//视频
public class VideoActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.mRecycle_view)
    RecyclerView mRecycleView;
    private BaseQuickAdapter videoAdapter;
    //视频列表
    private static List<VideoItem> vlist;
    @Override
    protected int bindLayout() {
        return R.layout.activity_video;
    }

    @Override
    public void initTopBar() {
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.app_color_blue));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topbar.setTitle(getResources().getString(R.string.video));
    }

    @Override
    public void initView() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        //设置条目分割线
        mRecycleView.addItemDecoration(new ItemDivider().setDividerWith(1).setDividerColor(getResources().getColor(R.color.color_999999)));
        //如果Item高度固定  增加该属性能够提高效率
        mRecycleView.setHasFixedSize(true);
        videoAdapter = new VideoAdapter(R.layout.item_video_list);
        //设置加载动画
        videoAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mRecycleView.setAdapter(videoAdapter);
    }

    @Override
    public void initData() {
        getData();
    }
    private void getData() {
        vlist = FileManager.getInstance().getVideos(this);
        videoAdapter.setNewData(vlist);
    }

    @Override
    public void initListener(){
        super.initListener();
        videoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                VideoItem item = (VideoItem) adapter.getItem(position);
//                showToast(VideoActivity.this,item.getName());
                Bundle bundle = new Bundle();
                bundle.putInt("id",item.getId());
                bundle.putString("title",item.getName());
                bundle.putString("videoUrl",item.getPath());

                Intent intent = getIntent();
                if (intent.getExtras()!=null) {
                    intent.putExtras(bundle);
                    setResult(Constant.ADDNOTE_VIDEO_RESULTCODE,intent);
                    finish();
                }else{
                    startActivity(PlayVideoActivity.class,bundle);
                }
            }
        });
    }
}
